package com.nidu.demo.file.storage.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.nidu.demo.common.enums.FileStorageTypeEnum;
import com.nidu.demo.file.storage.FileStorage;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * MinIO文件存储实现
 *
 * @author auto
 */
@Slf4j
@Component
public class MinioFileStorage implements FileStorage {

    @Override
    public String upload(MultipartFile file, String path, Map<String, Object> config) {
        try {
            // 获取配置参数
            String endpoint = (String) config.get("endpoint");
            String accessKey = (String) config.get("accessKey");
            String secretKey = (String) config.get("secretKey");
            String bucketName = (String) config.get("bucketName");
            
            // 创建MinIO客户端
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
            
            // 生成文件名
            String originalFilename = file.getOriginalFilename();
            String extension = "";
            if (originalFilename != null && originalFilename.contains(".")) {
                extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            String fileName = IdUtil.fastSimpleUUID() + extension;
            
            // 构建对象名
            String objectName = path + "/" + fileName;
            
            // 上传文件
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
            
            return objectName;
            
        } catch (Exception e) {
            log.error("MinIO文件上传失败", e);
            throw new RuntimeException("文件上传失败", e);
        }
    }

    @Override
    public void delete(String path, Map<String, Object> config) {
        try {
            String endpoint = (String) config.get("endpoint");
            String accessKey = (String) config.get("accessKey");
            String secretKey = (String) config.get("secretKey");
            String bucketName = (String) config.get("bucketName");
            
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
            
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(path)
                            .build()
            );
            
        } catch (Exception e) {
            log.error("MinIO文件删除失败", e);
            throw new RuntimeException("文件删除失败", e);
        }
    }

    @Override
    public String getUrl(String path, Map<String, Object> config) {
        String endpoint = (String) config.get("endpoint");
        String bucketName = (String) config.get("bucketName");
        String customDomain = (String) config.get("customDomain");
        
        if (StrUtil.isNotEmpty(customDomain)) {
            return customDomain + "/" + path;
        } else {
            return endpoint + "/" + bucketName + "/" + path;
        }
    }

    @Override
    public Integer getStorageType() {
        return FileStorageTypeEnum.MINIO.getType();
    }
}
